package com.whp.openapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whp.openapi.common.ResponseResult;
import com.whp.openapi.common.ResultTable;
import com.whp.openapi.dao.ApiMappingMapper;
import com.whp.openapi.entity.ApiMapping;
import com.whp.openapi.service.ApiMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author whp
 * @date 2021/11/26
 */
@Service
public class ApiMappingServiceImpl implements ApiMappingService {
    
    @Autowired
    private ApiMappingMapper apiMappingMapper;

    @Override
    public ResultTable table(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit, 
                             String name, 
                             Integer state) {
        PageHelper.startPage(page, limit);
        List<ApiMapping> apiMappings = apiMappingMapper.getTable(name, state);
        PageInfo pi = new PageInfo(apiMappings);
        return new ResultTable(0, "msg", (int) pi.getTotal(), pi.getList());
    }

    @Override
    public ResponseResult delApiMapping(Integer[] ids) {
        int i = apiMappingMapper.delApiMapping(ids);
        if (i > 0) {
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("删除失败");
    }
    

    @Override
    public ResponseResult addApiMapping(ApiMapping apiMapping) {
        if (apiMapping != null) {
            int i = apiMappingMapper.insertSelective(apiMapping);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("请提供信息");
    }

    @Override
    public ResponseResult updateApiMapping(ApiMapping apiMapping) {
        if (apiMapping != null) {
            int i = apiMappingMapper.updateByPrimaryKeySelective(apiMapping);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("请提供信息");
    }
}
